package com.xycake.controller.client;

import com.xycake.entity.User;
import com.xycake.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

//登录注册的controller
@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @PostMapping("/reg")
    public Map<String, Object> reg(User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", clientService.regUser(user));
        return map;
    }

    @PostMapping("/login")
    public Map<String, Object> login(User user, HttpSession httpSession) {
        Map<String, Object> map = new HashMap<>();
        User u = clientService.userLogin(user);
        if (u == null) {
            map.put("msg", "用户名或密码错误！");
        } else {
            httpSession.setAttribute("loginUser",u);
            map.put("msg", "success");
        }
        return map;
    }

    @RequestMapping("/logout")
    public Map<String, Object> logout(HttpSession httpSession) {
        httpSession.invalidate();
        Map<String, Object> map = new HashMap<>();
        return map;
    }


//    public Map<String ,Object> reg(){
//        Map<String ,Object> map=new HashMap<>();
//        return map;
//    }
}
